package com.rc.saas.tenant.service.ugs;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.utils.StringUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.mapper.member.MemberInfoMapper;
import com.rc.saas.tenant.mapper.ugs.UgsMemberSuperLevelMapper;
import com.rc.saas.tenant.mapper.ugs.UgsMemberSuperLevelOrderMapper;
import com.rc.saas.tenant.model.member.MemberInfo;
import com.rc.saas.tenant.model.ugs.*;
import com.rc.saas.tenant.search.ugs.UgsMemberSuperLevelSearch;
import com.rc.saas.tenant.vo.ugs.UgsMemberSuperLevelResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author frog
 * @date 2021/4/9
 */
@Service
public class UgsMemberSuperLevelServiceImpl implements UgsMemberSuperLevelService {

    @Autowired
    private UgsMemberSuperLevelMapper ugsMemberSuperLevelMapper;

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Autowired
    private UgsMemberSuperLevelOrderMapper ugsMemberSuperLevelOrderMapper;

    @Override
    public UgsMemberSuperLevelMapper getMapper() {
        return ugsMemberSuperLevelMapper;
    }

    @Override
    public PageInfo<UgsMemberSuperLevelResultVo> listPageUgsMemberSuperLevel(UgsMemberSuperLevelSearch ugsMemberSuperLevelSearch) {
        PageHelper.startPage(ugsMemberSuperLevelSearch.getOffset(), ugsMemberSuperLevelSearch.getLimit());
        List<UgsMemberSuperLevelResultVo> ugsMemberLevelRecords = ugsMemberSuperLevelMapper.listPageUgsMemberSuperLevel(ugsMemberSuperLevelSearch);
        return new PageInfo<UgsMemberSuperLevelResultVo>(ugsMemberLevelRecords);
    }

    @Override
    public TResponse saveGiveSuperLevel(List<Long> memberIdList, Integer days, String levelType, String tenantCode) {

        UgsMemberSuperLevelOrder ugsMemberSuperLevelOrder = new UgsMemberSuperLevelOrder();
        ugsMemberSuperLevelOrder.setTenantCode(ugsMemberSuperLevelOrder.getTenantCode());
        ugsMemberSuperLevelOrder.setSuperOrderType("1");
        ugsMemberSuperLevelOrder.setSuperOrderSource("0");
        ugsMemberSuperLevelOrder.setStatus("1");
        ugsMemberSuperLevelOrder.setSuperOrderNum("0");
        ugsMemberSuperLevelOrder.setPaymentType("0");
        ugsMemberSuperLevelOrder.setSuperLevelType(levelType);
        ugsMemberSuperLevelOrder.setSuperLevelPriceId(0L);

        if ("V1".equals(levelType)) {
            ugsMemberSuperLevelOrder.setSuperCardType("0");
        } else if ("V2".equals(levelType)) {
            ugsMemberSuperLevelOrder.setSuperCardType("1");
        } else if ("V3".equals(levelType)) {
            ugsMemberSuperLevelOrder.setSuperCardType("2");
        }


        Date date = new Date();

        UgsMemberSuperLevel ugsMemberSuperLevel = new UgsMemberSuperLevel();
        ugsMemberSuperLevel.setTenantCode(tenantCode);
        ugsMemberSuperLevel.setRecordType("1");
        ugsMemberSuperLevel.setSuperLevelType(levelType);
        ugsMemberSuperLevel.setTotalAmount(new BigDecimal(0));
        ugsMemberSuperLevel.setBeginTime(date);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        ugsMemberSuperLevel.setEndTime(calendar.getTime());

        ugsMemberSuperLevel.setStatus("0");

        StringBuilder failMobile = new StringBuilder();
        for (Long memberId : memberIdList) {
            UgsMemberSuperLevelExample example = new UgsMemberSuperLevelExample();
            example.createCriteria().andMemberIdEqualTo(memberId);
            List<UgsMemberSuperLevel> ugsMemberSuperLevels = ugsMemberSuperLevelMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(ugsMemberSuperLevels)) {
                if (!"1".equals(ugsMemberSuperLevels.get(0).getStatus())) {
                    if (ugsMemberSuperLevels.get(0).getEndTime().getTime() > System.currentTimeMillis()) {
                        if (!ugsMemberSuperLevels.get(0).getSuperLevelType().equals(levelType)) {
                            MemberInfo memberInfo = memberInfoMapper.selectByPrimaryKey(memberId);
                            failMobile.append(memberInfo.getMobile()).append(",");
                        }
                    }
                }
            }
        }
        if (StringUtil.isNotBlank(failMobile.toString())) {
            return TResponse.FAIL(String.format("%s已存在不同种类的会员卡", failMobile));
        }

        for (Long memberId : memberIdList) {
            UgsMemberSuperLevelExample example = new UgsMemberSuperLevelExample();
            example.createCriteria().andMemberIdEqualTo(memberId);
            List<UgsMemberSuperLevel> ugsMemberSuperLevels = ugsMemberSuperLevelMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(ugsMemberSuperLevels)) {

                ugsMemberSuperLevel.setMemberId(memberId);
                ugsMemberSuperLevelMapper.insertSelective(ugsMemberSuperLevel);

                ugsMemberSuperLevelOrder.setMemberId(memberId);
                ugsMemberSuperLevelOrder.setBeginTime(date);
                ugsMemberSuperLevelOrder.setEndTime(calendar.getTime());
                ugsMemberSuperLevelOrderMapper.insertSelective(ugsMemberSuperLevelOrder);
            } else {
                Date endTime = ugsMemberSuperLevels.get(0).getEndTime();

                ugsMemberSuperLevelOrder.setBeginTime(date);
                ugsMemberSuperLevelOrder.setEndTime(calendar.getTime());

                ugsMemberSuperLevels.get(0).setRecordType("1");
                ugsMemberSuperLevels.get(0).setSuperLevelType(levelType);
                ugsMemberSuperLevels.get(0).setBeginTime(new Date());
                ugsMemberSuperLevels.get(0).setEndTime(calendar.getTime());

                //判断是否存在不同种类型 为过期的会员卡
                if (!"1".equals(ugsMemberSuperLevels.get(0).getStatus())) {
                    if (ugsMemberSuperLevels.get(0).getEndTime().getTime() > System.currentTimeMillis()) {
                        if (!ugsMemberSuperLevels.get(0).getSuperLevelType().equals(levelType)) {
                            MemberInfo memberInfo = memberInfoMapper.selectByPrimaryKey(memberId);
                            return TResponse.FAIL(String.format("%s已存在不同种类的会员卡", memberInfo.getMobile()));
                        } else {
                            Calendar cal = Calendar.getInstance();
                            cal.setTime(endTime);
                            cal.add(Calendar.DATE, days);


                            //有同种会员增加时间
                            ugsMemberSuperLevels.get(0).setEndTime(cal.getTime());
                            ugsMemberSuperLevels.get(0).setSuperLevelType(levelType);

                            ugsMemberSuperLevelOrder.setBeginTime(endTime);
                            ugsMemberSuperLevelOrder.setEndTime(cal.getTime());

                        }
                    }
                }
                ugsMemberSuperLevels.get(0).setStatus("0");
                ugsMemberSuperLevelMapper.updateByPrimaryKey(ugsMemberSuperLevels.get(0));

                ugsMemberSuperLevelOrder.setMemberId(memberId);
                ugsMemberSuperLevelOrderMapper.insertSelective(ugsMemberSuperLevelOrder);
            }
        }

        return TResponse.SUCCESS_DATA("操作成功");
    }

    @Override
    public void updateBatchForcedFailure(List memberIds) {
        UgsMemberSuperLevelExample example = new UgsMemberSuperLevelExample();
        example.createCriteria().andMemberIdIn(memberIds);
        List<UgsMemberSuperLevel> ugsMemberSuperLevels = ugsMemberSuperLevelMapper.selectByExample(example);
        Date date = new Date();
        if (!CollectionUtils.isEmpty(ugsMemberSuperLevels)) {
            for (UgsMemberSuperLevel ugsMemberSuperLevel : ugsMemberSuperLevels) {
                ugsMemberSuperLevel.setStatus("1");
                ugsMemberSuperLevel.setEndTime(date);
                ugsMemberSuperLevelMapper.updateByPrimaryKey(ugsMemberSuperLevel);
            }
        }
    }
}
